<!DOCTYPE html>
<html lang="en">

{include file="Public/head"}

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>

<div class="am-g tpl-g">

    {include file='Public:header'}
    {include file='Public:menu2'}



    <!-- 内容区域 -->
    <div class="tpl-content-wrapper" >
        <div class="row-content am-cf">
            <div class="row">

                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title am-fl" onclick="aa()">编辑商品</div>
                            <div class="widget-function am-fr">
                                <div class="navbar-form pull-right margin">
                                    <a href="javascript:history.go(-1)" data-toggle="tooltip" title="" class="btn btn-default" data-original-title="返回"><i class="fa fa-reply"></i></a>
                                </div>
                            </div>
                        </div>
                        <div class="panel-body" style="">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#tab_tongyong" data-toggle="tab" aria-expanded="true">通用信息</a></li>
                                <li class=""><a href="#tab_goods_images" data-toggle="tab" aria-expanded="false">商品相册</a></li>
                                <li class=""><a href="#tab_goods_attr" data-toggle="tab" aria-expanded="false">商品属性</a></li>
                                <li class=""><a href="#tab_goods_spec" data-toggle="tab" aria-expanded="false">商品规格</a></li>


                            </ul>
                            <!--表单数据-->
                            <form  method="post" form="submit" id="addEditGoodsForm" style="">


                                <div class="tab-content" style="">
                                    <!--通用信息-->
                                    <div class="tab-pane active" id="tab_tongyong" style="">

                                        <table class="table table-bordered" style="">
                                            <tbody style="">
                                            <tr>
                                                <td>商品名称:</td>
                                                <td>
                                                    <input type="text" value="{$goods.goods_name}" name="goods_name" class="form-control" style="width:550px;">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>商品简介:</td>
                                                <td>
                                                    <textarea rows="3" cols="80" name="goods_remark" >{$goods.goods_remark}</textarea>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>商品货号</td>
                                                <td>
                                                    <input type="text" value="{$goods.goods_sn}" name="goods_sn" class="form-control" style="width:350px;">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>商品分类:</td>
                                                <td id="classdiv">
                                                    {foreach name="classifyd" key="k" item="v"}
                                                    {notempty name="$v"}
                                                    <div class="am-u-sm-2">
                                                        <select name="class[]" id="cat_id" onchange="subclass(this);" class="form-control" style="margin-left:-15px; background-color: #3330">
                                                            <option value="0">请选择分类</option>
                                                            {foreach name="v" key="key" item="value"}

                                                            <option value="{$key}" {notempty name="$parent[$k+1]"}{if $parent[$k+1]=="$key"} selected="selected" {/if} {/notempty}>{$value}</option>

                                                            {/foreach}
                                                        </select>
                                                    </div>
                                                    {/notempty}
                                                    {/foreach}
                                                </td>
                                            </tr>


                                            <tr>
                                                <td>商品品牌:</td>
                                                <td>
                                                    <select name="brand_id" id="brand_id" class="form-control" style="width:250px;">
                                                        <option value="">所有品牌</option>
                                                        {foreach name="brand" key="k" item="v"}
                                                        <option value="{$k}" {if $goods.brand_id=="$k"} selected="selected" {/if}>{$v} </option>
                                                        {/foreach}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>本店售价:</td>
                                                <td>
                                                    <input type="text" value="{$goods.shop_price}" name="shop_price" class="form-control" style="width:150px;" >
                                                    <span id="err_shop_price" style="color:#F00; display:none;"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>市场价:</td>
                                                <td>
                                                    <input type="text" value="{$goods.market_price}" name="market_price" class="form-control" style="width:150px;">
                                                    <span id="err_market_price" style="color:#F00; display:none;"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>内部价:</td>
                                                <td>
                                                    <input type="text" value="{$goods.cost_price}" name="cost_price" class="form-control" style="width:150px;">
                                                    <span id="err_cost_price" style="color:#F00; display:none"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>第一上级佣金:</td>
                                                <td>
                                                    <input type="number" value="{$goods.commission1}" name="commission1" class="form-control"  style="width:150px;">
                                                    <span id="err_cost_price" style="color:#F00; display:none"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>第二上级佣金价:</td>
                                                <td>
                                                    <input type="number" value="{$goods.commission2}" name="commission2" class="form-control" style="width:150px;">
                                                    <span id="err_cost_price" style="color:#F00; display:none"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>上传商品图片(1:1):</td>
                                                <td>
                                                    <input type="file" value="" name="original_img" id="image1" onchange="img(this)"  accept="image/jpg,image/jpeg,image/png,image/PNG" class="form-control large" style="width:500px;margin-top:10px; "/>
                                                    <img src="{$goods.original_img}" id="img2"style="width: 200px;text-align: center;"/>
                                                    <span id="err_original_img" style="color:#F00; display:none;"></span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>商品重量:</td>
                                                <td>
                                                    <input type="text" class="form-control" style="width:150px;" value="{$goods.weight}" name="weight" >
                                                    &nbsp;克 (以克为单位)
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>是否包邮:</td>
                                                <td>
                                                    是:<input type="radio" value="1" name="is_free_shipping" {if $goods.is_free_shipping=="1"}checked="checked"{/if}>
                                                    否:<input type="radio" value="0" name="is_free_shipping" {if $goods.is_free_shipping=="0"}checked="checked"{/if}>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>库存数量:</td>
                                                <td>
                                                    <input type="text" value="{$goods.store_count}" class="form-control" style="width:150px;" name="store_count">
                                                    <span id="err_store_count" style="color:#F00; display:none;"></span>
                                                </td>
                                            </tr>
                                            <!--<tr>-->
                                                <!--<td>赠送积分:</td>-->
                                                <!--<td>-->
                                                    <!--<input type="text" class="form-control" style="width:150px;" value="$goods.give_integral" name="give_integral">-->
                                                    <!--<span id="err_give_integral" style="color:#F00; display:none;"></span>-->
                                                <!--</td>-->
                                            <!--</tr>-->
                                            <!--<tr>-->
                                                <!--<td>兑换积分:</td>-->
                                                <!--<td>-->
                                                    <!--<input type="text" class="form-control" style="width:150px;" value="" name="exchange_integral" >-->
                                                    <!--<span id="err_exchange_integral" style="color:#F00; display:none;"></span>-->
                                                <!--</td>-->
                                            <!--</tr>-->
                                            <tr>
                                                <td>商品关键词:</td>
                                                <td>
                                                    <input type="text" class="form-control" style="width:550px;" value="{$goods.keywords}" name="keywords">
                                                    <span id="err_keywords" style="color:#F00; display:none;"></span>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <!--通用信息-->

                                    <!-- 商品相册-->
                                    <div class="tab-pane " id="tab_goods_images">
                                        <table class="table table-bordered">
                                            <tbody>
                                            <tr>
                                                <td id="imgtd">
                                                    {notempty name="images"}
                                                    {foreach name="images" key="k" item="v"}
                                                    <div class="goods_xc" >
                                                        <div class="am-u-sm-8 img_xc" >
                                                            <input type="file" value="" name="image[]"  onchange="imgPreview(this)"  accept="image/jpg,image/jpeg,image/png,image/PNG" class="form-control" style="width:500px;margin-top:10px; display: none"/>
                                                            <input class="imgurls" type="hidden" name="image_url[]" value="{$v.image_url}">
                                                            <img src="{$v.image_url}" style="width: 200px;text-align: center;"/>

                                                            <a  style="" onclick="remimgs(this)">&nbsp;删除</a>
                                                        </div>
                                                    </div>
                                                    {/foreach}

                                                    {/notempty}

                                                </td>


                                            </tr>

                                            </tbody>

                                        </table>
                                        <img src="/images/add-button.jpg" onclick="subimgs()" width="100" height="100">

                                    </div>
                                    <!--商品相册-->

                                    <!-- 商品属性-->
                                    <div class="tab-pane " id="tab_goods_attr">
                                        <table class="table table-bordered" id="goods_attr_table">
                                            <tbody >
                                            <tr>
                                                <td>所选商品属性:</td>
                                                <td>

                                                    <select name="goods_type" id="goods_type" class="form-control" onchange="inputAttr(this.value)" style="width:250px;">
                                                        <option value="0">选择商品类型</option>
                                                        {notempty name="type"}
                                                        {foreach name="type" key="k" item="v"}{$v.id}{$goods.goods_type}
                                                        <option value="{$v.id}"{if $goods.goods_type==$v.id} selected="selected" {/if}>{$v.name}</option>
                                                        {/foreach}
                                                        {/notenpty}
                                                    </select>

                                                </td>
                                            </tr>
                                            </tbody>
                                            <tbody id="tbodyAttr">
                                                {notempty name="attr"}
                                                {foreach name="attr" key="k" item="v"}
                                                <tr>
                                                    <td> {$v.attr_name}</td>
                                                    <td><input type="text" size="40"{notempty name="$goods_attr[$v.attr_id]"} value="{$goods_attr[$v.attr_id]}"{/notempty} name="attr[{$v.attr_id}]"></td>
                                                </tr>
                                                {/foreach}
                                                {/notempty}
                                            </tbody>
                                            <!--<tr >-->
                                                <!--<td> 属性名称</td>-->
                                                <!--<td><input type="text" size="40" value="" name="brand_name"></td>-->
                                            <!--</tr>-->
                                            <!--<tr >-->
                                                <!--<td> 品牌</td>-->
                                                <!--<td><input type="text" size="40" value="" name="brand"></td>-->
                                            <!--</tr>-->

                                        </table>
                                    </div>
                                    <!-- 商品属性-->

                                    <!--商品规格-->
                                    <div class="tab-pane" id="tab_goods_spec">
                                        <table class="table table-bordered" id="goods_spec_table">
                                            <tbody><tr>
                                                <td>商品类型:</td>
                                                <td>
                                                    <select name="spec_type" id="spec_type" class="form-control" onchange="buttonSpec(this.value)"  style="width:250px;">
                                                        <option value="0">选择商品类型</option>
                                                        {notempty name="$type.0"}
                                                        {foreach $type as $k=>$v}
                                                        <option value="{$v.id}" {if $goods.spec_type==$v.id} selected="selected" {/if}>{$v.name}</option>
                                                        {/foreach}
                                                        {/notenpty}
                                                    </select>
                                                </td>
                                            </tr>

                                            </tbody>
                                        </table>
                                        <div id="ajax_spec_data">
                                            <table class="table table-bordered" id="goods_spec_table1">
                                                <tbody id="soecTbody">
                                                </tbody>

                                            </table>

                                        </div>
                                        <div id="goods_spec_table2">
                                            <table class="table table-bordered" id="spec_input_tab">
                                                <tbody id="inputSpec">
                                                <tr>
                                                    {notempty name="$spec"}
                                                    {foreach name="spec" key="k" item="v"}
                                                    <td><b>{$v.spec_name}</b></td>
                                                    {/foreach}
                                                    {/notempty}
                                                    <td><b>原价</b></td>
                                                    <td><b>现价</b></td>
                                                    <td><b>库存</b></td>
                                                    <td><b>内部价</b></td>
                                                    <td><b>操作</b></td>
                                                </tr>
                                                {notempty name="$goods_spec"}
                                                {foreach name="goods_spec" key="k" item="v"}
                                                <tr>
                                                    {notempty name="$spec"}
                                                    {foreach name="spec" key="key" item="value"}
                                                    <td><b>

                                                        <select name="spec[{$k}][key][{$value.s_id}]">
                                                            {foreach name="value.item" key="key2" item="val2"}
                                                            <option value="{$key2}" {notempty name="$v['key'][$key]" }{if in_array($key2,$v['key'])} selected="selected"{/if}{/notempty}>{$val2}</option>
                                                            {/foreach}
                                                        </select>
                                                    </b></td>
                                                    {/foreach}
                                                    {/notempty}
                                                    <td><b><input calss="form-control" type="number" name="spec[{$k}][price_tall]"     value="{$v.price_tall}"></b></td>
                                                    <td><b><input calss="form-control" type="number" name="spec[{$k}][price]"          value="{$v.price}"></b></td>
                                                    <td><b><input calss="form-control" type="number" name="spec[{$k}][store_count]"    value="{$v.store_count}" count="count"></b></td>
                                                    <td><b><input calss="form-control" type="number" name="spec[{$k}][price_internal]" value="{$v.price_internal}"></b></td>
                                                    <td><b><a onclick="del_tbody(this);" class="tpl-table-black-operation-del"><i class="am-icon-trash"></i> 删除</a></b></td>
                                                </tr>
                                                {/foreach}
                                                {/notempty}
                                                </tbody>
                                            </table>
                                        </div>
                                        <h2 onclick="inputSpecbody()" style="">+</h2>

                                    </div>

                                    <!--商品规格-->

                                </div>
                                <div class="pull-right">
                                    <input type="hidden" name="goods_id" value="{$goods.goods_id}"> <!--编辑需要-->

                                    <input  class="btn btn-primary"  title="" data-toggle="tooltip" type="submit"  value="保存">
                                </div>
                            </form><!--表单数据-->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

</body>
{include file='Public:script'}
{include file='Public:validation'}
<script>

    function imgPreview(fileDom) {
        //判断是否支持FileReader
        if(window.FileReader) {
            var reader = new FileReader();
        } else {
            alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
        }
        //获取文件
        var file = fileDom.files[0];
        var imageType = /^image\//;
        //是否是图片
        if(!imageType.test(file.type)) {
            alert("请选择图片！");
            return;
        }
        //读取完成

        reader.onload = function(e) {
            //获取图片dom

            // var img = document.getElementById("img3");//原
            var img =fileDom;
            //图片路径设置为读取的图片
            $(fileDom).siblings('img').attr('src',e.target.result);
            // $(fileDom).siblings('.imgurls').val(e.target.result);
            $(fileDom).siblings('img').css("display", "block");
            $(fileDom).siblings('a').css("display", "inline-block")
        };
        reader.readAsDataURL(file);

        //添加新的上传图片位置

    }
    function img(fileDom) {
        //判断是否支持FileReader
        if(window.FileReader) {
            var reader = new FileReader();
        } else {
            alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
        }
        //获取文件
        var file = fileDom.files[0];
        var imageType = /^image\//;
        //是否是图片
        if(!imageType.test(file.type)) {
            alert("请选择图片！");
            return;
        }
        //读取完成
        reader.onload = function(e) {
            //获取图片dom
            var img = document.getElementById("img2");
            //图片路径设置为读取的图片
            img.src = e.target.result;
            $("#img2").css("display", "block")
        };
        reader.readAsDataURL(file);
    }
   $(function () {
       $(document).on('input propertychange','input[count="count"]' ,function() {
           $count_y=$('[name="store_count"]').val();
           $count=0;
           $("input[count=\"count\"]").each(function(){
               $cou=$(this).val();
               $count+=Number($cou)
           });
           if ($count>$count_y){
               layer.msg('规格总库'+$count+'存超过设定库存'+$count_y, {shade: 0.5, time: 2000,icon: 2});
               return false;
           }
       });

    //点击切换不同的输入
        $('[data-toggle="tab"]').click(function () {
            // console.log($(this).attr('href'));
            $('[data-toggle="tab"]').parent().removeClass('active');//样式初始化
            $(this).parent().addClass('active');//选中改变样式
            $('[data-toggle="tab"]').attr('aria-expanded',false);//属性初始化
            $(this).attr('aria-expanded',true);//选中属性变true
            // $('[data-toggle="tab"]').
            $(".tab-pane").removeClass('active');//详情初始化
            $($(this).attr('href')).addClass('active');//选中打开详情
        });

       var param = new Object();
       param['type_id']= {$goods.spec_type};
       $.post("{:url('buttonSpec')}",param, function (res) {
           $('#soecTbody').append(res);
       });



   });

    //提交
    function roleSubmit(){
        var form=document.getElementById("addEditGoodsForm");
        var formData=new FormData(form);
        $.ajax({
            url:"{:Url('addData')}",
            type:'POST',
            data:formData,
            async: false,
            cache: false,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success:function(res){
                if (res.code != 200) {
                    layer.msg(res.msg, {shade: 0.5, time: 2000,icon: 2});
                    return false;
                }
                layer.msg(res.msg, {shade: 0.5, time: 2000, icon: 1}, function(){
                    window.location.href=document.referrer;

                });
            }
        });
    }

    /*
     *    选中分类开启子分类选项
     */
    function subclass(v) {
        $(v).parent().nextAll().remove();//删除后面子分类

        if (0==v.value){
            return false;
        }
        var param = new Object();
        param['parent_id']   = v.value;
        $.post("{:url('subclassGoods')}",param, function (res) {
            $('#classdiv').append(res);
        });

    }
    //点击添加相片位置
    function subimgs() {
        var res=' <div class="goods_xc" >\n' +
            '                                                        <div class="am-u-sm-8" >\n' +
            '                                                            <input type="file"  value="" name="image[]"  onchange="imgPreview(this)"  accept="image/jpg,image/jpeg,image/png,image/PNG" class="form-control large" style="width:500px;margin-top:10px; "/>\n' +
            '                                                            <a  onclick="remimgs(this)" >&nbsp;删除</a>\n' +

            '                                                            <img src="" id="img4"style="width: 200px;text-align: center;display: none"/>\n' +
            '\n' +
            '                                                        </div>\n' +
            '                                                    </div>';
        $('#imgtd').append(res);

    }
    //点击删除相片位置
    function remimgs(t) {
        $(t).parent().parent().remove();
    }
    //生成属性填写框
    function inputAttr(t){
        $('#tbodyAttr').children().remove();
        if (0==t){
            return false;
        }
        var param = new Object();
        param['type_id']= t;
        $.post("{:url('inputAttr')}",param, function (res) {
            $('#tbodyAttr').append(res);
        });
    }
    //根据类型生成规格选择按钮
    function buttonSpec(t) {
        $('#soecTbody').children().remove();
        $('#inputSpec').children().remove();
        if (0==t){
            return false;
        }
        var param = new Object();
        param['type_id']= t;
        $.post("{:url('buttonSpec')}",param, function (res) {
            $('#soecTbody').append(res);
        });
        $.post("{:url('inputSpec')}",param, function (res) {
            $('#inputSpec').html(res);
        });
        $("h2").css("display", "block");
        inputSpecbody();

    }


    //点击增加添加商品规格的输入栏
    function inputSpecbody(){
        var param = new Object();

        param['type_id']=$('[name="spec_type"]').val() ;
        console.log(param['type_id']);
        $.post("{:url('inputSpecbody')}",param, function (res) {
            $('#inputSpec').append(res);
        });
    }

//删除商品规格详情
    function del_tbody(t) {
        $(t).parent().parent().parent().remove();
    }
</script>
<script type="text/javascript" src="/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    //var ue = UE.getEditor('container');
    var ue = UE.getEditor('container', {
        autoHeightEnabled:false,
        initialFrameHeight:420
    });
</script>
<script src="/js/global.js"></script>
<script src="/js/myFormValidation.js"></script>
<script src="/js/myAjax.js"></script>
<script src="http://cdn.bootcss.com/amazeui/2.7.2/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
</html>